<template>
  <div class="wrapperC">
    <div class="navList">
      <span>C类特配订单信息</span>
      <div><a-input-search placeholder="请输入搜索关键词" style="width: 400px" @search="onSearch" /></div>
    </div>
    <a-spin :spinning='showTable'>
      <div class="searInfo">
        <a-form class="advanced-form" :form="form" labelAlign="left" v-bind="formLayout">
          <a-row :gutter="24">
            <a-col :span="12">
              <a-form-item label="C类消息通知编号">
                <!-- <a-input v-model="form.cnumber" placeholder="请选择C类消息通知编号" readonly @click="getNoticeCode"/> -->
                <a-input-search v-model="form.cnumber" placeholder="请输入C类消息通知编号" @search="onSearchNum" />
                <!-- <a-select style="width: 100%" placeholder="请选择" v-model="form.cnumberInfo" :allowClear="true" show-search :filter-option="filterOption" @change="searchRes">
                  <a-select-option v-for="(item,index) in typeList" :key="index" :value="item.value">{{item.cnumber}}</a-select-option>
                </a-select> -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item :label="labelName.ztName">
                <a-input v-model="form.subject" placeholder="请输入主题" disabled/>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row :gutter="24" style="top:-10px;">
            <a-col :span="12">
              <a-form-item label="订单号" style="margin-bottom:10px;">
                <a-input v-model="form.orderNumber" placeholder="请输入订单号" disabled/>
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item :label="labelName.hhName" style="margin-bottom:10px;">
                <a-input v-model="form.lineNumber" placeholder="请输入行号" disabled/>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="tableTree">
        <common-table-tree
          ref="table"
          v-if="searchData.length"
          :columns="columns"
          :tableData="searchData"
          :selectType="'checkbox'"
          :data-row-key="'key'"
          :isRowsSelectVisible="true"
          :loadVisible="loadVisible"
          :isDisable="isDisable"
          scrollY="calc(100vh - 500px)" 
          :filterData="filterDataTable"
          :delSelectRow="delSelectRow"
          @selectedRows="selectedRows"
          @expandClick="expandClickJG"
          @onselectChange="onselectChange"
          @onSelectAll="onSelectAll"/>
          <common-table-tree ref="table" v-else :columns="columns" :tableData="searchData"/>
      </div>
      <div class="btnFooter">
        <a-button type="primary" @click="sureBtn">确定</a-button>
      </div>
    </a-spin>
    <a-modal title="提示" :visible="visible" @cancel="visible = false" :footer="null">
      <a style="padding-bottom:20px;">{{msgRes}}</a>
    </a-modal>
    <common-modal
      modalTitle="C类消息通知编号列表"
      :modalVisible="addMemberAddDialog"
      @handCancel="addMemberAddDialog=false"
      @searchRes="searchRes"
      :selectType="'radio'"
      :itemArr="typeList"
      :itemArrCopy="typeListCopy"
    ></common-modal>
  </div>
</template>

<script>
import commonTableTree from '@/components/common/commonTableTree';
// import { getNoticeCode,getPartDetail,getSecondSonList,saveCBom } from './exception'
import commonModal from "./commonModal.vue";
import { arrList,typeArrList } from "./dataBase";
import _ from "lodash";
export default {
  data(){
    return{
      showTable:false,
      visible:false,
      addMemberAddDialog:false,
      isDisable:'0',
      msgRes:'',
      formLayout:{
        labelCol:{span:4},
        wrapperCol:{span:20}
      },
      labelName:{
        ztName:'\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0'+"主题",
        hhName:'\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0'+"行号",
      },
      form:{
        cnumber:'',
        subject:'',
        orderNumber:'',
        lineNumber:'',
        oid:'',
      },
      typeList:[],
      typeListCopy:[],
      columns: [
        {
          displayName: "编号",
          dataIndex: "number",
          name: "number",
          fieldType: "Input",
          dataType: "String",
          width: 250,
          scopedSlots: {
            // filterDropdown: "filterDropdown",
            // filterIcon: "filterIcon",
            customRender: "number"
          }
        },
        {
          displayName: '名称',
          dataIndex: 'name',
          name: 'name',
          key: 'name',
          fieldType: "Input",
          dataType: "String",
          width: 200,
          ellipsis: true,
          scopedSlots: {
            filterDropdown: "filterDropdown",
            filterIcon: "filterIcon",
            customRender: "name"
          }
        },
        {
          displayName: '版本',
          dataIndex:'version',
          name:'version',
          key:'version',
          ellipsis: true,
          scopedSlots: { customRender: "version" }
        },
        {
          displayName: '新编号',
          dataIndex:'newNumber',
          name:'newNumber',
          key:'newNumber',
          width: 200,
          scopedSlots: { customRender: "newNumber" },
        },
        {
          displayName: '状态',
          dataIndex:'stateKey',
          name:'stateKey',
          key:'stateKey',
          fieldType: "Input",
          dataType: "String",
          scopedSlots: { customRender: "stateKey" }
        },
        {
          displayName: '图号',
          dataIndex:'drawingNo',
          name:'drawingNo',
          key:'drawingNo',
          scopedSlots: { customRender: "drawingNo" }
        },
        {
          displayName: '数量',
          dataIndex:'amount',
          name:'amount',
          key:'amount',
          scopedSlots: { customRender: "amount" }
        },
        {
          displayName: '单位',
          dataIndex:'unit',
          name:'unit',
          key:'unit',
          scopedSlots: { customRender: "unit" }
        },
        {
          displayName: '位置',
          dataIndex:'location',
          name:'location',
          key:'location',
          scopedSlots: { customRender: "location" }
        },
      ],
      columnsData:[],
      searchData:[],
      loadVisible:false,
      filterDataTable:{},
      childIdList:[],
      delSelectRow:[],//要删除的id
    }
  },
  components: { commonTableTree,commonModal },
  mounted(){
    // this.getNoticeCode();
    this.getGroupList();
  },
  methods:{
    selectedMember(val){
      console.log(200,val)
    },
    //获取C类消息通知编号
    getNoticeCode(){
      if(this.typeList.length){
        this.addMemberAddDialog = true;
      }else{
        this.showTable= true;
        getNoticeCode({number:this.form.cnumber ? this.form.cnumber : '*'}).then(res=>{
          if(res.data.success && res.data.success=="true"){
            this.typeList = res.data.data.data.map(item=>{
              item.value = JSON.stringify(item)
              return item;
            })
            this.typeListCopy = this.typeList;
            this.showTable = false;
            this.addMemberAddDialog = true;
          }else{
            this.typeList = []
            this.$message.error(res.data.message)
          }
        })
      }
    },
    //搜索订单号
    onSearchNum(val){
      this.typeList = typeArrList.map(item=>{
        item.value = JSON.stringify(item)
        return item;
      });
      this.typeListCopy = this.typeList;
      this.addMemberAddDialog = true;
      return false;
      this.showTable= true;
      getNoticeCode({number:this.form.cnumber ? '*'+this.form.cnumber+'*' : '*'}).then(res=>{
        if(res.data.success && res.data.success=="true"){
          this.typeList = res.data.data.data.map(item=>{
            item.value = JSON.stringify(item)
            return item;
          })
          this.typeListCopy = this.typeList;
          this.showTable = false;
          this.addMemberAddDialog = true;
        }else{
          this.typeList = []
          this.$message.error(res.data.message)
        }
      })
    },
    //根据编号获取主题订单号行号
    searchRes(val){
      if(val){
        let info = JSON.parse(val);
        this.form.cnumber = info.cnumber;
        this.form.subject = info.subject;
        this.form.orderNumber = info.orderNumber;
        this.form.lineNumber = info.lineNumber;
        this.form.oid = info.oid;
        this.isDisable = '1';
        if(this.$refs.table.selectedRowKeys.length){
          let newArr = _.clone(this.searchData);
          this.searchData = this.formatSetAll(newArr,0,0)
          this.$refs.table.selectedRowKeys = []
        }
      }else{
        this.form.cnumber = '';
        this.form.subject = '';
        this.form.orderNumber = '';
        this.form.lineNumber = '';
        this.form.oid = '';
        this.isDisable = '0';
        this.$refs.table.selectedRowKeys = []
        if(this.$refs.table.selectedRowKeys.length){
          let newArr = _.clone(this.searchData);
          this.searchData = this.formatSetAll(newArr,0,0)
        }
      }
      this.addMemberAddDialog = false;
      // console.log(222,this.form)
    },
    //获取结构列表
    getGroupList(){
      this.searchData = arrList;
      return false;
      this.showTable = true;
      getPartDetail({code:sessionStorage.getItem('number')}).then(res=>{
        this.showTable = false;
        if(res.data.success){
          // this.columnsData = res.data.data ? res.data.data : [];
          this.columnsData = res.data.data ? (typeof res.data.data == 'string' ? JSON.parse(res.data.data) : res.data.data) : [];
          if(this.columnsData.length){
            this.searchData = this.columnsData.map((item)=>{
              item.expanded = true;
              item.oid = 'OR:wt.part.WTPart:'+item.ida2a2;
              item.id = item.id ? item.id.toString() : '';
              item.lineNumber = item.lineNumber ? item.lineNumber.toString() : '';
              item.linkOid = item.linkOid ? 'OR:wt.part.WTPartUsageLink:'+item.linkOid.toString() : '';
              item.amount = item.amount ? item.amount.toString() : '';
              item.key = item.uuid;
              item.parentId = 'root';
              item.children = [];
              item.reuse = 'Yes';
              // item.leaf = true;
              item.amount = item.amount ? item.amount : '';
              item.icon = '';
              item.version = item.version+(item.iteration ? '.' : '')+item.iteration;
              item.unit = item.unit ? item.unit : "";
              item.expanded = false;
              item.newNumber = item.newNumber ? item.newNumber : '';
              item.cls = '';
              item.rule = '';
              item.checked = '';
              delete item.containerId
              delete item.createStampa
              delete item.endItem
              delete item.genericType
              delete item.updateStampa
              return item;
            });
            if(this.searchData.length){
              this.expandClickJG(false,this.searchData[0])
            }
            // console.log(220,this.searchData)
          }else{
            this.searchData = []
          }
        }else{
          this.columnsData = [];
          this.searchData = [];
          this.$message.error(res.data.message)
        }
        // console.log(this.columnsData)
      }).catch(()=>{
        this.showTable = false;
      })
    },
    format(arr,id,val) {
      for (var i = 0; i < arr.length; i++) {
        if(arr[i].uuid == id){
          arr[i].expanded = true;
          arr[i].children = val
          // arr[i].leaf = arr[i].children.length ? false : true
        }
        if (arr[i].children && arr[i].children) {
          arr[i].children = this.format(arr[i].children,id,val);
        }
      }
      return arr;
    },
    //点击结构列表展示子级
    expandClickJG(expanded,row){
      // console.log(296,this.$refs.table.expandedRowKeys)
      if(row.children && row.children.length) return;
      this.showTable = true;
      getSecondSonList({oid:row.ida2a2}).then(res=>{
        // let arr = res.data.data ? res.data.data : [];
        // console.log(300,res.data.data)
        let arr = res.data.data ? (typeof res.data.data == 'string' ? JSON.parse(res.data.data) : res.data.data) : [];
        // console.log(302,arr)
        if(arr.length){
           arr = arr.map((item)=>{
            item.expanded = false;
            item.oid = 'OR:wt.part.WTPart:'+item.ida2a2;
            item.id = item.id ? item.id.toString() : '';
            item.lineNumber = item.lineNumber ? item.lineNumber.toString() : '';
            item.linkOid = item.linkOid ? 'OR:wt.part.WTPartUsageLink:'+item.linkOid.toString() : '';
            item.amount = item.amount ? item.amount.toString() : '';
            item.key = item.uuid;
            item.children = [];
            item.parentId = row.ida2a2 ? row.ida2a2.toString() : '';
            item.parentAllId = row.parentAllId ? row.parentAllId+','+row.uuid : row.uuid
            item.newNumber = item.newNumber ? item.newNumber : '';
            // item.leaf = true;
            item.reuse = 'Yes';
            item.icon = '';
            item.checked = '';
            item.rule = '';
            item.version = item.version+(item.iteration ? '.' : '')+item.iteration;
            item.expanded = false;
            item.cls = '';
            delete item.containerId
            delete item.createStampa
            delete item.endItem
            delete item.genericType
            delete item.updateStampa
            return item;
          })
          let newArr = _.clone(this.searchData)
          this.searchData = this.format(newArr,row.uuid,arr)
          // console.log(268,this.searchData)
        }
        this.showTable = false;
      }).catch(()=>{
        this.showTable = false;
      })
    },
    formatSetValue(arr,id,flag) {
      //flag  0取消选中   1选中    
      for (var i = 0; i < arr.length; i++) {
        if(arr[i].uuid == id){
          arr[i].newNumber = flag==1 ? (arr[i].leaf ? '':(arr[i].number+(this.form.orderNumber?'_':'')+this.form.orderNumber+(this.form.lineNumber?'_':'')+this.form.lineNumber)) : ''
          arr[i].reuse = flag==1 ? '' : 'Yes'
        }
        if (arr[i].children && arr[i].children.length) {
          arr[i].children = this.formatSetValue(arr[i].children,id,flag);
        }
      }
      return arr;
    },
    //单个选择
    onselectChange(selected,record){
      // console.log(399,record)
      if(!this.form.cnumber){
        this.$message.error('请先选择C类消息通知编号！')
        this.$refs.table.selectedRowKeys = []
        return false;
      }
      let newArr = _.clone(this.searchData);
      let i = 0;
      selected ? i=1 : i=0;
      this.searchData = this.formatSetValue(newArr,record.uuid,i)
      if(selected){
        let arr = record.parentAllId ? record.parentAllId.split(',') : [];
        if(arr.length){
          arr.map(item=>{
            this.searchData = this.formatSetValue(newArr,item,i)
          })
        }
      }else{
        if(record.children && record.children.length){
          this.childIdList = [];
          this.findAllChildId(record.children);
          this.delSelectRow = this.childIdList
          // console.log(this.childIdList)
          // this.childIdList =  ["74544352-262b-4fce-bd3b-4f172d721710", "158cdf48-5744-4ce1-bf41-8d7d051be3a3", "25ab4260-19b0-4eb1-8831-bbfc167164d2", "e80c6437-5d2d-464b-b996-8d1fdf1fa023", "297c0f98-c0b8-4c13-9c3a-24a408426fce", "86649c4a-b341-44c2-be8b-1c44df596fb7"]
          this.childIdList.map(item=>{
            this.searchData = this.formatSetValue(newArr,item,0)
          })
        }
      }
    },
    //找到所有的子集id
    findAllChildId(arr){
      arr.map(item=>{
        this.childIdList.push(item.uuid)
        if(item.children && item.children.length){
          this.findAllChildId(item.children)
        }
      })
    },
    formatSetAll(arr,flag,val) {
      //flag  0取消选中   1选中    
      //val是否改变选中
      for (var i = 0; i < arr.length; i++) {
        arr[i].defaultChecked = flag==1 ? true : false;
        arr[i].newNumber = flag==1 ? (arr[i].leaf?'':(arr[i].number+(this.form.orderNumber?'_':'')+this.form.orderNumber+(this.form.lineNumber?'_':'')+this.form.lineNumber)) : ''
        arr[i].reuse = flag==1 ? '' : 'Yes'
        if (arr[i].children && arr[i].children.length) {
          arr[i].children = this.formatSetAll(arr[i].children,flag,val);
        }
      }
      return arr;
    },
    //全选  反选
    onSelectAll(selected){
      if(!this.form.cnumber){
        this.$message.error('请先选择C类消息通知编号！')
        this.$refs.table.selectedRowKeys = []
        return false;
      }
      let newArr = _.clone(this.searchData);
      let i = selected ? 1 : 0;
      this.searchData = this.formatSetAll(newArr,i,1)
    },
    selectedRows(val){},
    sureBtn(){
      if(!this.form.cnumber){
        this.$message.error('请先选择C类消息通知编号！')
        this.$refs.table.selectedRowKeys = []
        return false;
      }
      if(!this.$refs.table.selectedRowKeys.length){
        this.$message.error('请至少勾选一项！')
        return false;
      }
      let obj = {
        oid:this.form.oid,
        cnumber:this.form.cnumber,
        orderNumber: this.form.orderNumber,
        copy:'false',
        subject: this.form.subject,
        lineNumber: this.form.lineNumber,
        partData:this.searchData[0]
      }
      let param={
        oid:this.searchData[0].oid,
        newPartNumber:this.searchData[0].number+'_'+this.form.orderNumber+'_'+this.form.lineNumber,
        jsonData:obj
      }
      // console.log(JSON.stringify(param))
      param.jsonData = JSON.stringify(obj)
      saveCBom(param).then(res=>{
        this.msgRes = res.data.msg;
        this.visible = true;
      })
    },
    onSearch(value) {
      if(value){
        this.searchData = [];
        this.searchData = this.columnsData.filter(item=>{
          return JSON.stringify(item).indexOf(value)!=-1;
        })
      }else{
        this.searchData = this.columnsData;
      }
    },
    //条件筛选过滤
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      );
    },
  }
}
</script>

<style lang="less" scoped>
  .wrapperC{
    height: calc(100vh - 165px);
    background: #fff;
    padding: 20px 0;
    .navList{
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1px solid #ddd;
      padding: 0 20px 10px;
      span{
        font-size: 18px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #333333;
        line-height: 24px;
      }
    }
    .searInfo{
      padding: 14px 20px 0;
    }
    .tableTree{
      padding: 20px;
      margin-top: -30px;
    }
    .btnFooter{
      position: fixed;
      bottom:20px;
      right: 30px;
    }
  }
</style>